
package com.dy.net;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;

import com.dy.config.CreatConfig;
import com.dy.config.MysqlConfig;
import com.dy.config.MysqlSelectSql;
import com.dy.config.SelectSql;
import com.dy.converter.MysqlTypeConverter;
import com.dy.converter.TypeConverter;
import com.dy.model.Column;
import com.dy.model.TableInfo;

import cn.dy.util.StringUtil;

/**
 * @Description:
 * @Date：2018年6月28日 @author： DongY
 * @version ：1.0 1.[2018年6月28日]CreatBy：dy
 */

public class MysqlTableInfo implements InitTableInfo {
	@Override
	public TableInfo getTableInfos(final MysqlConfig config, final CreatConfig creatConfig)
			throws SQLException, ClassNotFoundException {
		final String tableName = creatConfig.getTableName();
		final TableInfo tableInfo = new TableInfo(tableName);
		if (!StringUtil.isEmpty(creatConfig.getClassName())) {
			tableInfo.setClassName(creatConfig.getClassName());
		}
		final Connection conn = DoConnection.getConnection(config);
		final Statement statement = conn.createStatement();
		final SelectSql selectSql = new MysqlSelectSql();
		ResultSet set = statement.executeQuery(selectSql.columnSql(tableName));
		final List<Column> columns = new ArrayList<>();
		final boolean notAssign = StringUtil.isEmpty(creatConfig.getPriKeyName());
		final Set<String> importSet = new HashSet<>();
		System.out.println("用户自定义主键名:\t" + creatConfig.getPriKeyName());
		while (set.next()) {
			final Column col = new Column();
			col.setName(set.getString(1));
			String remark = set.getString(9);
			if (StringUtil.isEmpty(remark)) {
				remark = set.getString(1);
			}
			col.setRemark(remark);
			final TypeConverter converter = new MysqlTypeConverter();
			col.setJdbcType(set.getString(2));
			final Class<?> javaType = converter.converter(set.getString(2));
			if (!"java.lang".equals(javaType.getPackage().getName().trim())) {
				importSet.add(javaType.getName());
			}
			col.setJavaType(javaType);
			final String pri = set.getString(5);
			final boolean isPri = (pri != null) && "pri".equals(pri.toLowerCase());
			col.setPRI(isPri);
			final String increment = set.getString(7);// 扩展字段
			if (Objects.equals("auto_increment", increment)) {
				col.setIscrement(true);
			}

			if (col.isPRI()) {
				if (notAssign) {
					tableInfo.setPriKeyColumn(col.getJavaName());
					tableInfo.setPriKeyType(col.getJavaType());
				} else {
					if (Objects.equals(col.getName(), creatConfig.getPriKeyName())) {
						tableInfo.setPriKeyColumn(col.getJavaName());
						tableInfo.setPriKeyType(col.getJavaType());
					}
				}
			}
			columns.add(col);

		}
		tableInfo.setImportSet(importSet);
		set = statement
				.executeQuery(MessageFormat.format(selectSql.tableInfoSql(tableName), tableName, config.getDbName()));
		while (set.next()) {
			final String tableType = set.getString(2);
			if ("BASE TABLE".equals(tableType)) {
				tableInfo.setRemark(set.getString(3));
			}
			tableInfo.setTableType(tableType);
		}
		tableInfo.setColumns(columns);
		return tableInfo;
	}

}
